package spe.ui.editor.policy;

import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.gef.EditPart;
import org.eclipse.gef.commands.Command;
import org.eclipse.gef.editpolicies.XYLayoutEditPolicy;
import org.eclipse.gef.requests.CreateRequest;

import spe.model.notation.SPEMDiagram;
import spe.model.spem.NodeElement;
import spe.ui.editor.command.ElementChangeLayoutCommand;
import spe.ui.editor.command.ElementCreatingCommand;

public class DiagramXYLayoutPolicy extends XYLayoutEditPolicy 
{

	/*
	 * After click on an item on palette to create new object,
	 * this method will be called every mouse moving on the part
	 * that means there are many ElementCreateCommand object will be created
	 * */
	@Override
	protected Command getCreateCommand(CreateRequest rq) 
	{
		ElementCreatingCommand cmd = null;
		if(rq.getNewObject() instanceof NodeElement)
		{
			cmd = new ElementCreatingCommand();
			cmd.setLocation(rq.getLocation());
			cmd.setParent((SPEMDiagram)(getHost().getModel()));
			cmd.setElement((NodeElement)(rq.getNewObject()));
			//cmd.setEditPart(getHost());
			//System.out.println(String.format("New Object: %s; Diagram: %s; Location: %s ", rq.getNewObject(), getHost().getModel(), rq.getLocation()));
		}
		return cmd;
	}
	
	@Override
	protected Command createChangeConstraintCommand(EditPart child, Object constraint)
	{
		ElementChangeLayoutCommand command = new ElementChangeLayoutCommand();
		command.setElement( (NodeElement) child.getModel() );
		command.setPosition(((Rectangle)constraint).getLocation());
		return command;
	}

}
